package com.wxg.zxing.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQRCode {

	public static void main(String[] args) {
		MultiFormatReader reader = new MultiFormatReader();
		
		Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
		hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
		
		try {
			File file = new File("C:/Users/wxg/AppData/Local/Temp/QrCode_1080846216891635.png");
			BufferedImage image = ImageIO.read(file);
			
			BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
			
			Result result = reader.decode(binaryBitmap, hints);
			
			System.out.println( String.format("解析结果：%s", result.toString()) );
			System.out.println( String.format("二维码格式类型：%s", result.getBarcodeFormat()) );
			System.out.println( String.format("二维码文本内容：%s", result.getText()) );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
